<?php
namespace App\Models;

use App\Config\Database;
use PDO;
use PDOException;

/**
 * 群组模型
 * @package App\Models
 * @description:
 * 1. 创建群组
 * 2. 获取群组信息
 * 3. 更新群组信息
 * 4. 解散群组
 * 5. 搜索群组
 */
class Group {
    private $conn;
    private $table = 'groups';

    public $id;
    public $group_id;
    public $name;
    public $avatar_url;
    public $owner_id;
    public $member_count;
    public $announcement;
    public $is_dismissed;
    public $created_at;
    public $updated_at;

    public function __construct() {
        $database = new Database();
        $this->conn = $database->getConnection();
    }

    /**
     * 创建新群组
     * @return bool
     */
    public function create() {
        try {
            $query = "INSERT INTO " . $this->table . " 
                (group_id, name, avatar_url, owner_id, member_count, announcement, is_dismissed) 
                VALUES 
                (:group_id, :name, :avatar_url, :owner_id, :member_count, :announcement, :is_dismissed)";
            
            $stmt = $this->conn->prepare($query);
            
            // 绑定参数
            $stmt->bindParam(':group_id', $this->group_id);
            $stmt->bindParam(':name', $this->name);
            $stmt->bindParam(':avatar_url', $this->avatar_url);
            $stmt->bindParam(':owner_id', $this->owner_id);
            $stmt->bindParam(':member_count', $this->member_count);
            $stmt->bindParam(':announcement', $this->announcement);
            $stmt->bindParam(':is_dismissed', $this->is_dismissed);
            
            // 设置默认值
            $this->member_count = 1; // 创建者是第一个成员
            $this->is_dismissed = 0;
            
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Create group error: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 通过ID获取群组信息
     * @param int $id 群组ID
     * @return array|null
     */
    public function findById($id) {
        try {
            $query = "SELECT * FROM " . $this->table . " WHERE id = :id LIMIT 1";
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
            
            return $stmt->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            error_log("Find group by ID error: " . $e->getMessage());
            return null;
        }
    }

    /**
     * 通过群组ID获取群组信息
     * @param string $groupId 群组唯一标识
     * @return array|null
     */
    public function findByGroupId($groupId) {
        try {
            $query = "SELECT * FROM " . $this->table . " WHERE group_id = :group_id LIMIT 1";
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':group_id', $groupId);
            $stmt->execute();
            
            return $stmt->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            error_log("Find group by group ID error: " . $e->getMessage());
            return null;
        }
    }

    /**
     * 更新群组信息
     * @return bool
     */
    public function update() {
        try {
            $query = "UPDATE " . $this->table . " 
                      SET name = :name, 
                          avatar_url = :avatar_url, 
                          announcement = :announcement,
                          updated_at = CURRENT_TIMESTAMP
                      WHERE id = :id";
            
            $stmt = $this->conn->prepare($query);
            
            // 绑定参数
            $stmt->bindParam(':id', $this->id);
            $stmt->bindParam(':name', $this->name);
            $stmt->bindParam(':avatar_url', $this->avatar_url);
            $stmt->bindParam(':announcement', $this->announcement);
            
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Update group error: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 解散群组
     * @param int $id 群组ID
     * @return bool
     */
    public function dismiss($id) {
        try {
            $query = "UPDATE " . $this->table . " 
                      SET is_dismissed = 1, updated_at = CURRENT_TIMESTAMP 
                      WHERE id = :id";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':id', $id);
            
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Dismiss group error: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 搜索群组
     * @param string $keyword 搜索关键词
     * @param int $limit 返回数量
     * @return array
     */
    public function search($keyword, $limit = 10) {
        try {
            $query = "SELECT id, group_id, name, avatar_url, member_count 
                      FROM " . $this->table . " 
                      WHERE name LIKE :keyword AND is_dismissed = 0 
                      LIMIT :limit";
            
            $stmt = $this->conn->prepare($query);
            $searchTerm = "%{$keyword}%";
            $stmt->bindParam(':keyword', $searchTerm);
            $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
            
            $stmt->execute();
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            error_log("Search groups error: " . $e->getMessage());
            return [];
        }
    }
}